<template>
  <el-dialog
    :title="!dataForm.wmNo ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="焊机编号" prop="wmId">
      <el-input v-model="dataForm.wmId" placeholder="焊机编号"></el-input>
    </el-form-item>
    <el-form-item label="焊机名称" prop="wmName">
      <el-input v-model="dataForm.wmName" placeholder="焊机名称"></el-input>
    </el-form-item>
    <el-form-item label="焊机电流" prop="wmCurrent">
      <el-input v-model="dataForm.wmCurrent" placeholder="焊机电流"></el-input>
    </el-form-item>
    <el-form-item label="焊机电压" prop="wmVoltage">
      <el-input v-model="dataForm.wmVoltage" placeholder="焊机电压"></el-input>
    </el-form-item>
    <el-form-item label="送丝速度" prop="wmSpeed">
      <el-input v-model="dataForm.wmSpeed" placeholder="送丝速度"></el-input>
    </el-form-item>
    <el-form-item label="温度" prop="wmTemperature">
      <el-input v-model="dataForm.wmTemperature" placeholder="温度"></el-input>
    </el-form-item>
    <el-form-item label="气体流量" prop="wmGas">
      <el-input v-model="dataForm.wmGas" placeholder="气体流量"></el-input>
    </el-form-item>
    <el-form-item label="电流故障" prop="wmCurrentState">
      <el-input v-model="dataForm.wmCurrentState" placeholder="电流故障"></el-input>
    </el-form-item>
    <el-form-item label="电压故障" prop="wmVoltageState">
      <el-input v-model="dataForm.wmVoltageState" placeholder="电压故障"></el-input>
    </el-form-item>
    <el-form-item label="送丝故障" prop="wmSpeedState">
      <el-input v-model="dataForm.wmSpeedState" placeholder="送丝故障"></el-input>
    </el-form-item>
    <el-form-item label="温度故障" prop="wmTemperatureState">
      <el-input v-model="dataForm.wmTemperatureState" placeholder="温度故障"></el-input>
    </el-form-item>
    <el-form-item label="气体流量故障" prop="wmGasState">
      <el-input v-model="dataForm.wmGasState" placeholder="气体流量故障"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          wmNo: 0,
          wmId: '',
          wmName: '',
          wmCurrent: '',
          wmVoltage: '',
          wmSpeed: '',
          wmTemperature: '',
          wmGas: '',
          wmCurrentState: '',
          wmVoltageState: '',
          wmSpeedState: '',
          wmTemperatureState: '',
          wmGasState: ''
        },
        dataRule: {
          wmId: [
            { required: true, message: '焊机编号不能为空', trigger: 'blur' }
          ],
          wmName: [
            { required: true, message: '焊机名称不能为空', trigger: 'blur' }
          ],
          wmCurrent: [
            { required: true, message: '焊机电流不能为空', trigger: 'blur' }
          ],
          wmVoltage: [
            { required: true, message: '焊机电压不能为空', trigger: 'blur' }
          ],
          wmSpeed: [
            { required: true, message: '送丝速度不能为空', trigger: 'blur' }
          ],
          wmTemperature: [
            { required: true, message: '温度不能为空', trigger: 'blur' }
          ],
          wmGas: [
            { required: true, message: '气体流量不能为空', trigger: 'blur' }
          ],
          wmCurrentState: [
            { required: true, message: '电流故障不能为空', trigger: 'blur' }
          ],
          wmVoltageState: [
            { required: true, message: '电压故障不能为空', trigger: 'blur' }
          ],
          wmSpeedState: [
            { required: true, message: '送丝故障不能为空', trigger: 'blur' }
          ],
          wmTemperatureState: [
            { required: true, message: '温度故障不能为空', trigger: 'blur' }
          ],
          wmGasState: [
            { required: true, message: '气体流量故障不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.wmNo = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.wmNo) {
            this.$http({
              url: this.$http.adornUrl(`/wmstate/weldingmachinestate/info/${this.dataForm.wmNo}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.wmId = data.weldingmachineState.wmId
                this.dataForm.wmName = data.weldingmachineState.wmName
                this.dataForm.wmCurrent = data.weldingmachineState.wmCurrent
                this.dataForm.wmVoltage = data.weldingmachineState.wmVoltage
                this.dataForm.wmSpeed = data.weldingmachineState.wmSpeed
                this.dataForm.wmTemperature = data.weldingmachineState.wmTemperature
                this.dataForm.wmGas = data.weldingmachineState.wmGas
                this.dataForm.wmCurrentState = data.weldingmachineState.wmCurrentState
                this.dataForm.wmVoltageState = data.weldingmachineState.wmVoltageState
                this.dataForm.wmSpeedState = data.weldingmachineState.wmSpeedState
                this.dataForm.wmTemperatureState = data.weldingmachineState.wmTemperatureState
                this.dataForm.wmGasState = data.weldingmachineState.wmGasState
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/wmstate/weldingmachinestate/${!this.dataForm.wmNo ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'wmNo': this.dataForm.wmNo || undefined,
                'wmId': this.dataForm.wmId,
                'wmName': this.dataForm.wmName,
                'wmCurrent': this.dataForm.wmCurrent,
                'wmVoltage': this.dataForm.wmVoltage,
                'wmSpeed': this.dataForm.wmSpeed,
                'wmTemperature': this.dataForm.wmTemperature,
                'wmGas': this.dataForm.wmGas,
                'wmCurrentState': this.dataForm.wmCurrentState,
                'wmVoltageState': this.dataForm.wmVoltageState,
                'wmSpeedState': this.dataForm.wmSpeedState,
                'wmTemperatureState': this.dataForm.wmTemperatureState,
                'wmGasState': this.dataForm.wmGasState
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
